/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *   02110-1301, USA.
 * 
 * $Id: MobileTemplate.java 49 2008-05-17 02:35:15Z jens464 $
*/
package albareth.core.entity.template;

import java.util.ArrayList;
import java.util.Iterator;

import albareth.core.action.Spell;
import albareth.core.action.SpellMagicMissile;
import albareth.core.brain.BossBrain;
import albareth.core.brain.Brain;
import albareth.core.brain.MonsterBrain;
import albareth.core.entity.Mobile;
import albareth.frontend.Tile;
import frosch.util.Log;
import frosch.util.Range;

public class MobileTemplate
{
    private String _name;
    private Tile _tile;
    
    private Range _levelRange;
    
    private Brain _brain = new MonsterBrain();
    
    //private Range _hitpointRange;
    //private Range _manaRange;
    
    private ArrayList _spells = new ArrayList();
    

    public MobileTemplate(String name, Tile tile)
    {
        _name = name;
        _tile = tile;
    }

    public Range getLevelRange()
    {
        return _levelRange;
    }

    public void setLevelRange(Range levelRange)
    {
        _levelRange = levelRange;
    }
    
    public void addSpell(Spell spell)
    {
        _spells.add(spell);
    }

    public Mobile create()
    {
        Mobile mob = new Mobile(_name, _tile);
        mob.setLevel(_levelRange.getRandomValue());
        mob.setMaxHitpoints(new Range(mob.getLevel() * 4, mob.getLevel() * 7).getRandomValue());
        mob.setHitpoints(mob.getMaxHitpoints());
        mob.setMaxMana(new Range(mob.getLevel() * 4, mob.getLevel() * 7).getRandomValue());
        mob.setMana(mob.getMaxMana());
        //derive brain:
        try {
            mob.setBrain((Brain)_brain.clone());
        } catch (CloneNotSupportedException e) {
            Log.error("Cloning of brain not supported: " + e);
        }
        //derive spells:
        for (Iterator it = _spells.iterator(); it.hasNext(); )
        {
            Spell spell = (Spell)it.next();
            try {
                mob.addSpell((Spell)spell.clone());
            } catch (CloneNotSupportedException e) {
                Log.error("Cloning of spell not supported: " + e);
            }
        }
        
        return mob;
    }
    
    public Mobile createBossVariant()
    {
        Mobile boss = create();
        boss.setBrain(new BossBrain());
        boss.setName(boss.getName() + " Boss");
        boss.setMaxHitpoints((int)(boss.getMaxHitpoints() * 3.5));
        boss.setHitpoints(boss.getMaxHitpoints());
        boss.setMaxMana((int)(boss.getMaxMana() * 3.5));
        boss.setMana(boss.getMaxMana());
        Spell bossMissile = new SpellMagicMissile();
        bossMissile.setCastingDuration(1000);
        bossMissile.setCooldownDuration(3000);
        boss.addSpell(bossMissile);
        boss.setBoss(true);
        // TODO boss special name and more stats...
        return boss;
    }



    public Brain getBrain()
    {
        return _brain;
    }


    public void setBrain(Brain brain)
    {
        _brain = brain;
    }


}
